<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ConfigController extends Controller
{
   
   //---------------------------------------------用户添加
    


        //执行修改
        public function postUpdate(Request $request)
        {
             // dd($request->all());
            //获取信息
            $id = $request->only('id');
            
            $data = $request->only('des','call','icpno','logo','title');

            //进行验证
            $this->validate($request,[

             'title' => 'required|max:25',
             'call' => 'required|max:11',
             'icpno' => 'required|max:255',
             'des' => 'required|max:255',  
            ],[
               'title.required' => '标题不能为空',
               'title.max' => '标题过长了',
               'call.required' => '内容不能为空',
               'call.max' => '内容过长了',
               'icpno.required' => '内容不能为空',
               'icpno.max' => '内容过长了',
               'des.required' => '内容不能为空',
               'des.max' => '内容过长了',
            ]);



            //logo上传
            if($request->hasFile('logo')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('logo')->getClientOriginalExtension();
           // echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('logo')->move('./uploads/',$name.'.'.$ext);
            //echo '<img src="/uploads/'.$name.'.jpg" alt="">';
            //$data['logo'] = '/uploads/'.$name.'.'.$ext;
                $data['logo'] = $name.'.'.$ext;
                 // dd($data);
            // dd(self::suofang('./uploads/'.$name.'.'.$ext,129,129));
            $img = self::suofang('./uploads/'.$name.'.'.$ext,129,110);
         }



            
     
            //执行
             $res = DB::table('config')->where('id',$id)->update($data);
              // var_dump($res);
               if($res){

                    return redirect('/admin/config/edit')->with('success','用户修改成功');
                    



                }else{
                     return back()->with('error','用户修改失败');
                }

        }


        //显示修改页面
        public function getEdit()
        {
            //echo "1";
            
            $config = DB::table('config')->get();
            //var_dump($config);die;
            return view('admin.config.edit',['config'=>$config]);
        }


        //缩放函数
        public static function suofang($spath, $sw = 100, $sh = 100,$pre = 's_')
                {
                    //1.创建画布
                    //计算比例  100 100  要缩放的图像的宽高  1920 1200  //用小的比例
                    $srcinfo = getimagesize($spath);
                    //根据图片类型 去创建图像资源 ...
                    $src = imagecreatefromjpeg($spath); //strrchr  aaaa.jpg
                    
                    if($sw/$srcinfo[0] > $sh/$srcinfo[1]){
                        $bili = $sh/$srcinfo[1];
                    }else{
                        $bili = $sw/$srcinfo[0];
                    }
                    //舍去法取整
                    $sw = floor($srcinfo[0] * $bili);
                    $sh = floor($srcinfo[1] * $bili);


                    $dst = imagecreatetruecolor($sw,$sh);

                    //3.开始绘画
                    imagecopyresampled($dst, $src, 0, 0, 0, 0, $sw, $sh, $srcinfo[0], $srcinfo[1]);

                    //5.将图像保存为文件
                    //随机文件名  //获取文件后缀名  目录
                    //$ext = strrchr($spath,'.');
                    //$filename = time().rand(1000,9999).$ext;
                    $filename = rtrim(dirname($spath),'/').'/'.$pre.basename($spath);
                    
                    imagejpeg($dst, $filename);

                    //6.销毁资源
                    imagedestroy($dst);
                    imagedestroy($src);
                    
                    //返回原文件名加前缀
                    return $pre.basename($spath);
                }

   


	
}
